﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TextMIS
{
    public partial class frmGroupItm : Form
    {
        public frmGroupItm()
        {
            InitializeComponent();
        }

        sysopr xtbiz = new sysopr();
        dbopr GroupItm = new dbopr();
        string str_JykCode = "";//检验科代码
        string str_state = "Update";
        private DataTable dtTjxmb;

        private void frmGroupItm_Load(object sender, EventArgs e)
        {
            //new Common.Common().AddImages(imageList1);
            //tv_tjlxb.ImageList = imageList1;
            DataBind();
        }
        //初始化展示列表
        void DataBind()
        {
            Tr_GrpLsit.Nodes.Clear();
            //str_JykCode = xtbiz.GetXtCsz("JykCode");//检验科代码

            TreeNode node = new TreeNode("体检科室");
            node.Tag = "0";
            DataTable dt_Bran = GroupItm.Get_Bran_list();
            foreach (DataRow dr in dt_Bran.Rows)
            {
                string str_Bran = dr["Bra_id"].ToString().Trim();//科室编号
                TreeNode node1 = new TreeNode(dr["Bra_name"].ToString().Trim());
                node1.Tag = str_Bran;

                //if (str_JykCode == str_Bran)
                //{
                //    DataTable dt_lclxb = GroupItm.Get_tj_lclxb();
                //    foreach (DataRow dr3 in dt_lclxb.Rows)
                //    {
                //        TreeNode node3 = new TreeNode(dr3["mc"].ToString().Trim());
                //        node3.Tag = dr3["lclx"].ToString().Trim();

                //        DataTable dt_tj_tjlxb = GroupItm.Get_tj_zhxm_hd(str_lxbh, node3.Tag.ToString());
                //        foreach (DataRow dr4 in dt_tj_tjlxb.Rows)
                //        {
                //            TreeNode node4 = new TreeNode(dr4["mc"].ToString().Trim());
                //            node4.Tag = dr4["bh"].ToString().Trim();//编号
                //            if (dr4["yxbz"].ToString().Trim() == "0")//停用
                //            {
                //                node4.ForeColor = Color.Red;
                //            }
                //            node3.Nodes.Add(node4);
                //        }
                //        node1.Nodes.Add(node3);
                //    }

                //}
                //else
                //{
                    DataTable dt_tjxmb = GroupItm.Get_GroupItm_list(str_Bran);
                    foreach (DataRow dr1 in dt_tjxmb.Rows)
                    {
                        TreeNode node2 = new TreeNode(dr1["Gro_name"].ToString().Trim());
                        node2.Tag = dr1["Gro_id"].ToString().Trim();
                        if (dr1["IsEnable"].ToString().Trim() == "0")//停用
                        {
                            node2.ForeColor = Color.Red;
                        }
                        node1.Nodes.Add(node2);
                    }
                //}

                node.Nodes.Add(node1);
            }
            Tr_GrpLsit.Nodes.Add(node);
            //AddImage(node);

            cb_Bran.SelectedIndexChanged -= new EventHandler(cmb_ksmc_SelectedIndexChanged);
            cb_Bran.DataSource = GroupItm.Get_Bran_list();
            cb_Bran.DisplayMember = "Bra_name";
            cb_Bran.ValueMember = "Bra_id";
            cb_Bran.SelectedIndexChanged += new EventHandler(cmb_ksmc_SelectedIndexChanged);

            //cmb_bblx.DataSource = xtbiz.GetXtZd(6);//标本类型
            cb_MuterType.DisplayMember = "xmmc";
            cb_MuterType.ValueMember = "bzdm";

            cb_CheckTyp.DataSource = xtbiz.GetDicInfo(9);//检查类型
            cb_CheckTyp.DisplayMember = "xmmc";
            cb_CheckTyp.ValueMember = "bzdm";

            cb_FeesTyp.DataSource = xtbiz.GetDicInfo(7);//收费类型
            cb_FeesTyp.DisplayMember = "xmmc";
            cb_FeesTyp.ValueMember = "bzdm";
        }
        void cmb_ksmc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (object.Equals(null, cb_Bran.SelectedValue)) return;
            if (str_JykCode == cb_Bran.SelectedValue.ToString().Trim())
            {
                cb_MedTyp.SelectedValueChanged -= new EventHandler(cmb_lclx_SelectedValueChanged);
                if (str_state == "Insert")
                    cb_MedTyp.Enabled = true;
                cb_MedTyp.DataSource = GroupItm.Get_tj_lclxb();
                cb_MedTyp.DisplayMember = "mc";
                cb_MedTyp.ValueMember = "lclx";
                cb_MedTyp.SelectedIndex = -1;
                cb_MedTyp.SelectedValueChanged += new EventHandler(cmb_lclx_SelectedValueChanged);
            }
            else
            {
                cb_MedTyp.DataSource = null;
                cb_MedTyp.Enabled = false;
                DG.DataSource = GroupItm.Get_Iteminfo_Bybar(cb_Bran.SelectedValue.ToString().Trim(), "");
            }
        }

        void cmb_lclx_SelectedValueChanged(object sender, EventArgs e)
        {
            //根据科室ID和临床类型获取项目
            if (object.Equals(null, cb_MedTyp.SelectedValue))
            {
                DG.DataSource = GroupItm.Get_Iteminfo_Bybar(cb_Bran.SelectedValue.ToString().Trim(), "");
            }
            else
            {
                DG.DataSource = GroupItm.Get_Iteminfo_Bybar(cb_Bran.SelectedValue.ToString().Trim(), cb_MedTyp.SelectedValue.ToString().Trim());
            }
        }
        private void rb_sfxxyb1_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_sfxybb1.Checked)
            {
                cb_MuterType.Enabled = true;
            }
        }

        private void rb_sfxxyb2_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_sfxybb2.Checked)
            {
                cb_MuterType.SelectedIndex = -1;
                cb_MuterType.Enabled = false;
            }
        }

        private void Tr_GrpLsit_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (object.Equals(null, Tr_GrpLsit.SelectedNode.Tag)) return;
            string str_bh = Tr_GrpLsit.SelectedNode.Tag.ToString().Trim();
            DataTable dt = GroupItm.Get_GrpItm_Info(str_bh);
            if (dt.Rows.Count < 1) return;

            // Bra_id,IsEnable,Gro_id,Disp_Order,Gro_name,dj,lclx,Pinyin_code,Wubi_code,Mid_Basis,TipInfo,Brife_sum,IsMuter,MuterTyp,Check_Type,Fees_type 
            cb_Bran.SelectedValue = dt.Rows[0]["Bra_id"].ToString().Trim();
            if (dt.Rows[0]["IsEnable"].ToString().Trim() == "1")//启用标志
                rb_yxbz1.Checked = true;
            else
                rb_yxbz2.Checked = true;
            txt_GrpId.Text = dt.Rows[0]["Gro_id"].ToString().Trim();
            txt_disp_order.Text = dt.Rows[0]["Disp_Order"].ToString().Trim();
            txt_GrpName.Text = dt.Rows[0]["Gro_name"].ToString().Trim();
            txt_Price.Text = dt.Rows[0]["dj"].ToString().Trim();
            cb_MedTyp.SelectedValue = dt.Rows[0]["lclx"].ToString().Trim();
            txt_defCode.Text = dt.Rows[0]["DefCode"].ToString().Trim();
            txt_Pinyin.Text = dt.Rows[0]["Wubi_code"].ToString().Trim();
            txt_Wubi.Text = dt.Rows[0]["Wubi_code"].ToString().Trim();
            txt_MidBasis.Text = dt.Rows[0]["Mid_Basis"].ToString().Trim();
            txt_Tipinfo.Text = dt.Rows[0]["TipInfo"].ToString().Trim();
            txt_BriefSum.Text = dt.Rows[0]["Brife_sum"].ToString().Trim();
            if (dt.Rows[0]["IsMuter"].ToString().Trim() == "1")//是否需要标本
                rb_sfxybb1.Checked = true;
            else
                rb_sfxybb2.Checked = true;
            cb_MuterType.SelectedValue = dt.Rows[0]["MuterTyp"].ToString().Trim();
            cb_CheckTyp.SelectedValue = dt.Rows[0]["Check_Type"].ToString().Trim();
            cb_FeesTyp.SelectedValue = dt.Rows[0]["Fees_type"].ToString().Trim();

            //根据科室ID获取具体项目
            dtTjxmb = new DataTable();
            dtTjxmb = GroupItm.Get_Iteminfo_Bybar(dt.Rows[0]["Bra_id"].ToString().Trim(), dt.Rows[0]["lclx"].ToString().Trim());
            DG.DataSource = dtTjxmb;
            DataTable dt_tj_zhxm_dt = GroupItm.Get_GrpItem_Rel(dt.Rows[0]["Gro_id"].ToString().Trim());//获取选中的
            foreach (DataRow dr in dt_tj_zhxm_dt.Rows)
            {
                string str_tjxm = dr["Itm_Id"].ToString().Trim();
                foreach (DataGridViewRow dgr in DG.Rows)
                {
                    if (dgr.Cells["Item_id"].Value.ToString().Trim() == str_tjxm)
                    {
                        dgr.Cells["xz"].Value = true;
                        DG.CurrentCell = dgr.Cells[0];
                        //改变选中的背景颜色                       
                    }
                }
            }
            ChangeColor();
            cb_Bran.Enabled = false;


            if (Tr_GrpLsit.SelectedNode.Nodes.Count == 0)
            {
                Tr_GrpLsit.SelectedNode.SelectedImageIndex = Tr_GrpLsit.SelectedNode.ImageIndex;
                ////tv_tjlxb.SelectedNode.Parent.ImageIndex = 0;
            }
        }

        void ChangeColor()
        {
            for (int i = 0; i < DG.Rows.Count; i++)
            {
                Boolean xz = Convert.ToBoolean(DG.Rows[i].Cells["xz"].Value);
                if (xz)
                {
                    DG.Rows[i].DefaultCellStyle.ForeColor = Color.Blue;
                }
                else
                {
                    DG.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
                }
            }
        }

        private void bt_all_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgr in DG.Rows)
            {
                dgr.Cells["xz"].Value = true;
            }
            ChangeColor();
        }

        private void bt_notall_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgr in DG.Rows)
            {
                dgr.Cells["xz"].Value = false;
            }
            ChangeColor();
        }
        //保存明细
        private void bt_savemx_Click(object sender, EventArgs e)
        {
            if (txt_GrpId.Text.Trim() == "") return;

            


            dbopr SaveGprRel = new dbopr();
            SaveGprRel.str_Sqltxt("delete GrpItemRel where Grp_Id='" + txt_GrpId.Text.Trim() + "'");
            foreach (DataGridViewRow dgr in DG.Rows)
            {
                if (Convert.ToBoolean(dgr.Cells["xz"].Value))
                {
                    string itmid = dgr.Cells["Item_id"].Value.ToString().Trim();
                    string braid = cb_Bran.SelectedValue.ToString().Trim();

                    SaveGprRel.str_Insert_GrpItemRel(txt_GrpId.Text.Trim(), braid, itmid, "1");
                }
            }
            SaveGprRel.Exec_ArryList();
            MessageBox.Show("保存明细成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        void ClearControl()
        {
            cb_Bran.SelectedIndex = -1;
            rb_yxbz1.Checked = true;
            rb_sfxybb1.Checked = true;
            txt_GrpId.Text = "";
            txt_disp_order.Text = "";
            txt_GrpName.Text = "";
            txt_Price.Text = "0.00";
            txt_defCode.Text = "";
            txt_Pinyin.Text = "";
            txt_Wubi.Text = "";
            txt_MidBasis.Text = "";
            txt_Tipinfo.Text = "";
            txt_BriefSum.Text = "";
            rb_sfxybb2.Checked = true;
            cb_MuterType.SelectedIndex = -1;
            cb_CheckTyp.SelectedIndex = -1;
            cb_FeesTyp.SelectedIndex = -1;
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            ClearControl();
            cb_Bran.Enabled = true;
            foreach (DataGridViewRow dgr in DG.Rows)
            {
                dgr.Cells["xz"].Value = false;
            }
            str_state = "Insert";
        }

        private void bt_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_qx_Click(object sender, EventArgs e)
        {
            ClearControl();
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            if (txt_GrpId.Text.Trim() == "") return;
            //注意业务项目引用了，就不允许删除，暂时没有控制**********************************************************************
            //if (GroupItm.Exists_Tjzhxm(txt_GrpId.Text.Trim()) > 0)
            //{
            //    MessageBox.Show("该项目被引用了，不允许删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            GroupItm.Delete_GroupItm(txt_GrpId.Text.Trim());
            MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Tr_GrpLsit.Nodes.Remove(Tr_GrpLsit.SelectedNode);
            ClearControl();
            dtTjxmb.Rows.Clear();
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            string str_yxbz = "1";//启用标志
            string str_sfxybb = "0";//是否需要标本
            string str_lclx = "";//临床类型
            string str_bblx = "";//标本类型
            string str_jyjclx = "";//检验检查类型
            string str_sflb = "";//收费类别
            if (rb_yxbz1.Checked) str_yxbz = "1";//启用
            if (rb_yxbz2.Checked) str_yxbz = "0";//停用
            if (rb_sfxybb1.Checked) str_sfxybb = "1";
            if (rb_sfxybb2.Checked) str_sfxybb = "0";

            if (object.Equals(null, cb_MedTyp.SelectedValue))
                str_lclx = "";
            else
                str_lclx = cb_MedTyp.SelectedValue.ToString().Trim();

            if (object.Equals(null, cb_MuterType.SelectedValue))
                str_bblx = "";
            else
                str_bblx = cb_MuterType.SelectedValue.ToString().Trim();
            if (txt_GrpName.Text.Trim() == "")
            {
                MessageBox.Show("请填写组合名称！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = txt_GrpName;
                return;
            }
            if (object.Equals(null, cb_CheckTyp.SelectedValue))
            {
                MessageBox.Show("请选择检查类型！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = cb_CheckTyp;
                return;
            }
            else
                str_jyjclx = cb_CheckTyp.SelectedValue.ToString().Trim();

            if (object.Equals(null, cb_FeesTyp.SelectedValue))
                str_sflb = "";
            else
                str_sflb = cb_FeesTyp.SelectedValue.ToString().Trim();

            if (str_state == "Update")
            {
                if (txt_GrpId.Text.Trim() == "") return;
                GroupItm.Update_tj_zhxm_hd(cb_Bran.SelectedValue.ToString().Trim(), str_yxbz, txt_GrpId.Text.Trim(), txt_disp_order.Text.Trim(), txt_GrpName.Text.Trim(),
                    txt_Price.Text.Trim(), str_lclx, txt_defCode.Text.Trim(), txt_Pinyin.Text.Trim(), txt_Wubi.Text.Trim(), txt_MidBasis.Text.Trim(), txt_Tipinfo.Text.Trim(),
                    txt_BriefSum.Text.Trim(), str_sfxybb, str_bblx, str_jyjclx, str_sflb);
                MessageBox.Show("修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (rb_yxbz2.Checked)
                {
                    Tr_GrpLsit.SelectedNode.ForeColor = Color.Red;
                    Tr_GrpLsit.SelectedNode.ImageIndex = 3;
                    Tr_GrpLsit.SelectedNode.SelectedImageIndex = 3;
                }
                else
                {
                    Tr_GrpLsit.SelectedNode.ForeColor = Tr_GrpLsit.Nodes[0].ForeColor;//停用
                    Tr_GrpLsit.SelectedNode.ImageIndex = 2;
                    Tr_GrpLsit.SelectedNode.SelectedImageIndex = 2;
                }
                Tr_GrpLsit.SelectedNode.Text = txt_GrpName.Text.Trim();
                return;
            }

            if (str_state == "Insert")
            {
                if (object.Equals(cb_Bran.SelectedValue, null)) return;

                //txt_bh.Text = GroupItm.Get_proc_get_tjzhxmbh(cmb_ksmc.SelectedValue.ToString().Trim());//获取体检项目的编号
                //获取项目组合编号
                txt_GrpId.Text = xtbiz.GetId("GrpItmId", 1);
                if (txt_disp_order.Text == "")
                    txt_disp_order.Text = txt_GrpId.Text;

                GroupItm.Insert_GrpItmInfo(cb_Bran.SelectedValue.ToString().Trim(), str_yxbz, txt_GrpId.Text.Trim(), txt_disp_order.Text.Trim(), txt_GrpName.Text.Trim(),
                    txt_Price.Text.Trim(), str_lclx, txt_defCode.Text.Trim(), txt_Pinyin.Text.Trim(), txt_Wubi.Text.Trim(), txt_MidBasis.Text.Trim(), txt_Tipinfo.Text.Trim(),
                    txt_BriefSum.Text.Trim(), str_sfxybb, str_bblx, str_jyjclx, str_sflb);

                MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                str_state = "Update";

                TreeNode node = new TreeNode(txt_GrpName.Text.Trim());
                node.Tag = txt_GrpId.Text.Trim();
                if (rb_yxbz2.Checked) node.ForeColor = Color.Red;//停用
                Tr_GrpLsit.SelectedNode.Parent.Nodes.Add(node);
                cb_Bran.Enabled = false;
                return;
            }
        }

        private void txt_mc_Leave(object sender, EventArgs e)
        {
            txt_Pinyin.Text = xtbiz.Get_Py(txt_GrpName.Text);
            txt_Wubi.Text = xtbiz.Get_Wb(txt_GrpName.Text);
        }

        private void dgv_tjxmb_DoubleClick(object sender, EventArgs e)
        {
            if (DG.SelectedRows.Count <= 0)
            {
                return;
            }

            DG.SelectedRows[0].Cells["xz"].Value = !Convert.ToBoolean(DG.SelectedRows[0].Cells["xz"].Value);
            ChangeColor();
        }



    }
}
